//
//  JKViewShopsDetailHeader.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKViewShopsDetailHeader.h"

// view
#import "ByViewStarMark.h"              // 星际
#import "ByViewDetailHeaderCarousel.h"
// vc
#import "ByUIPayVC.h"



#import "JZLocationConverter.h"

#import <MapKit/MapKit.h>



@implementation JKViewShopsDetailHeader
{
    UIImageView     *_iconView;
    ByViewDetailHeaderCarousel *_carousel;
    UILabel         *_titleLabel;
    UILabel         *_priceLabel;
    UILabel         *_discountLabel;        // 折扣
    ByViewStarMark  *_starView;
    UILabel         *_sellsPerMonthLabel;
    UIButton        *_buyButton;
    
    UILabel         *_addressLabel;
    
    UIImageView     *_distanceImageView;
    UILabel         *_distanceLabel;
}
- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 12;
        self.layer.masksToBounds = true;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _iconView = [UIImageView new];
    _iconView.image = DEFAULT_IMAGE;
    [self addSubview:_iconView];
    
    _carousel = [[ByViewDetailHeaderCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 20, (SCREEN_WIDTH - 20) / 2) timerDuration:3];
    [self addSubview:_carousel];
    
    _titleLabel = [UILabel new];
    _titleLabel.numberOfLines = 0;
    _titleLabel.text = @"标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题";
    _titleLabel.font = FONT(15);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.textColor = [UIColor blackColor];
    [self addSubview:_titleLabel];
    
    // 线条
    UIView *line1 = [UIView new];
    line1.backgroundColor = BY_LINE_COLOR;
    [self addSubview:line1];
    
    _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _buyButton.titleLabel.font = FONT(14);
    _buyButton.backgroundColor = rgba(118, 197, 250, 1);
    _buyButton.layer.cornerRadius = 4;
    _buyButton.layer.masksToBounds = true;
    [_buyButton setTitle:@"买单" forState:UIControlStateNormal];
    [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_buyButton addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_buyButton];
    
    _priceLabel = [UILabel new];
    _priceLabel.text = @"人均：￥69/人";
    _priceLabel.font = FONT(15);
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    _priceLabel.textColor = rgba(237, 102, 117, 1);
    [self addSubview:_priceLabel];
    
    _discountLabel = [UILabel new];
    _discountLabel.backgroundColor = rgba(237, 102, 117, 1);
    _discountLabel.text = @"8.8折";
    _discountLabel.font = FONT(13);
    _discountLabel.textAlignment = NSTextAlignmentCenter;
    _discountLabel.textColor = [UIColor whiteColor];
    [self addSubview:_discountLabel];
    
    _starView = [ByViewStarMark new];
    [_starView setupUI:0];
    [self addSubview:_starView];
    
    _sellsPerMonthLabel = [UILabel new];
    _sellsPerMonthLabel.text = @"年售60单";
    _sellsPerMonthLabel.font = FONT(15);
    _sellsPerMonthLabel.textAlignment = NSTextAlignmentLeft;
    _sellsPerMonthLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_sellsPerMonthLabel];
    
    // 线条
    UIView *line2 = [UIView new];
    line2.backgroundColor = BY_LINE_COLOR;
    [self addSubview:line2];
    
    _addressLabel = [UILabel new];
    _addressLabel.text = @"住址住址住址住址住址住址住址住址住址住址住址住址住址住址住址住址住址住址";
    _addressLabel.font = FONT(15);
    _addressLabel.numberOfLines = 0;
    _addressLabel.textAlignment = NSTextAlignmentLeft;
    _addressLabel.textColor = [UIColor blackColor];
    [self addSubview:_addressLabel];
    
    _distanceImageView = [UIImageView new];
    _distanceImageView.image = [UIImage imageNamed:@"商家-地址"];
    [self addSubview:_distanceImageView];
    
    UITapGestureRecognizer *addresstap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addressTap)];
    [_distanceImageView addGestureRecognizer:addresstap];
    _distanceImageView.userInteractionEnabled = true;
    
    _distanceLabel = [UILabel new];
    _distanceLabel.text = @"距离我1.1千米";
    _distanceLabel.font = FONT(13);
    _distanceLabel.textAlignment = NSTextAlignmentLeft;
    _distanceLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_distanceLabel];
    
    // 线条
    UIView *line3 = [UIView new];
    line3.backgroundColor = BY_LINE_COLOR;
    [self addSubview:line3];
  
    UIImageView *telView = [UIImageView new];
    telView.userInteractionEnabled = true;
    telView.image = [UIImage imageNamed:@"商家-打电话"];;
    [self addSubview:telView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tel)];
    [telView addGestureRecognizer:tap];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.width.equalTo(self);
        make.height.equalTo(_iconView.mas_width).multipliedBy(0.5);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(_iconView.mas_bottom).offset(10);
        make.right.equalTo(@-10);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(10);
        make.height.equalTo(@1);
    }];
    [_buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.width.equalTo(@70);
        make.height.equalTo(@30);
        make.top.equalTo(line1.mas_bottom).offset(20);
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line1);
        make.bottom.equalTo(_buyButton.mas_centerY).offset(-2);
    }];
    [_discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel.mas_right).offset(10);
        make.height.equalTo(@20);
        make.width.equalTo(@45);
        make.centerY.equalTo(_priceLabel);
    }];
    [_starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel);
        make.top.equalTo(_buyButton.mas_centerY).offset(2);
        make.width.equalTo(@80);
        make.height.equalTo(_starView.mas_width).multipliedBy(0.2);
    }];
    [_sellsPerMonthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_starView.mas_right).offset(20);
        make.centerY.equalTo(_starView);
    }];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(line1);
        make.top.equalTo(_buyButton.mas_bottom).offset(20);
    }];
    
    [telView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@30);
        make.top.equalTo(line2.mas_bottom).offset(20);
        make.right.equalTo(@-15);
    }];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(telView.mas_left).offset(-15);
        make.height.equalTo(@40);
        make.centerY.equalTo(telView);
        make.width.equalTo(@1);
    }];
    
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line2);
        make.right.equalTo(line3.mas_left).offset(-10);
        make.top.equalTo(line2.mas_bottom).offset(5);
    }];
    
    [_distanceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_addressLabel);
        make.top.equalTo(_addressLabel.mas_bottom).offset(10);
        make.width.height.equalTo(@15);
        
        // 设置高度
        make.bottom.equalTo(@-10);
    }];
    
    
    [_distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_distanceImageView.mas_right).offset(10);
        make.centerY.equalTo(_distanceImageView);
        make.height.equalTo(_distanceImageView);
        
    }];
}

- (void)setModel:(ByModelShopsDetail *)model {
    
    _model = model;
    ByModelShopsListItem *itemModel = model.shopsItemModel;
 
    
    [_iconView setImageWithURL:[NSURL URLWithString:itemModel.main_img_urlstr] placeholderImage:DEFAULT_IMAGE completed:nil];
    _carousel.imageURLStringDatas = itemModel.imgs;
    _titleLabel.text = itemModel.name;
    _priceLabel.text = [NSString stringWithFormat:@"人均：￥%@/人", itemModel.price];
    _discountLabel.text = [NSString stringWithFormat:@"%.1lf折", itemModel.discount_rate.floatValue * 10];
    if (itemModel.distance) {
        _distanceLabel.text = [NSString stringWithFormat:@"距离我%@米", itemModel.distance];
        _distanceLabel.hidden = false;
        _distanceImageView.hidden = false;
    } else {
        _distanceLabel.hidden = true;
        _distanceImageView.hidden = true;
    }
    _addressLabel.text = itemModel.position;
    _sellsPerMonthLabel.text = [NSString stringWithFormat:@"年售%@单", itemModel.monthly_sales];
    
    [_starView setupUI:itemModel.grade.floatValue / 5.0];
}

- (void)buy {
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self.viewController presentViewController:navi animated:YES completion:nil];
        return;
    }
    
    ByUIPayVC *vc = [[ByUIPayVC alloc] init];
    vc.detailModel = self.model;
    [((ByBaseVC *)self.viewController) by_normalPush:vc hasNavi:YES];
}
- (void)tel {
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@", _model.shopsItemModel.contact_number];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (void)addressTap {

    if (self.location == nil) {
        [ByCoreToolAlert showOnlyText:@"尚未获取到您当前位置，请稍后"];
        return;
    }
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加
    {
        double latitude = self.location.coordinate.latitude;
        double longitude = self.location.coordinate.longitude;
        NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%@,%@&mode=driving", latitude, longitude, self.model.shopsItemModel.lat, self.model.shopsItemModel.lng];
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *mapUrl = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:mapUrl];
        
    }else{
        
        [self onDaoHangForIOSMap];
    }
}
#pragma mark  导航 - iosMap
-(void)onDaoHangForIOSMap
{
    // 起点
    CLLocationCoordinate2D coor = [JZLocationConverter bd09ToGcj02:self.location.coordinate];
    MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor addressDictionary:nil]];
    // 目的地的位置
    CLLocation *location2 = [[CLLocation alloc]initWithLatitude:[self.model.shopsItemModel.lat floatValue] longitude:[self.model.shopsItemModel.lng floatValue]];
    CLLocationCoordinate2D coor3 = location2.coordinate;
    CLLocationCoordinate2D coor2 = [JZLocationConverter bd09ToGcj02:coor3];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor2 addressDictionary:nil]];

    NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];
    NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
    // 打开苹果自身地图应用，并呈现特定的item
    [MKMapItem openMapsWithItems:items launchOptions:options];
    
}


@end
